<div class="layuimini-main">
    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label">选择资源</label>
            <div class="layui-input-inline">
                <input type="file" id="upload-file" name="uploadFile" class="layui-input"/>
                <!--                    <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入相册标题"-->
                <!--                           class="layui-input">-->
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" lay-verify="required" lay-reqtext="资源标题不能为空" placeholder="请输入标题" value="" class="layui-input">
                <tip>填写班级的名称，如 19-3数据科学与大数据技术。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">资源简介</label>
            <div class="layui-input-block">
                <textarea id="description" name="describe" autocomplete="off" placeholder="请输入资源简介" class="layui-input"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">资源标签</label>
            <div class="layui-input-block">
                <select name="tag" lay-verify="" lay-search id="tag-select">
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否公开</label>
            <div class="layui-input-block">
                <input type="checkbox" name="isPublic" lay-skin="switch" lay-text="是|否">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">拍摄日期</label>
            <div class="layui-input-block">
                <input type="text" name="date" id="date" lay-verify="date" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">拍摄地点</label>
            <div class="layui-input-block">
                <input type="text" name="location" lay-verify="location" autocomplete="off" placeholder="请输入拍摄地点"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">班级人数</label>
            <div class="layui-input-block">
                <input type="text" name="totalnumber" lay-verify="totalnumber" autocomplete="off" placeholder="请输入班级人数"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">参与人数</label>
            <div class="layui-input-block">
                <input type="text" name="realnumber" lay-verify="realnumber" autocomplete="off" placeholder="请输入参与人数"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>

    </div>
    <fieldset>
                <legend>人员表</legend>
                <table id="oTable" class="layui-table">
                <!-- <thead>
                <tr>
                <th>第一列标题</th>
                <th>第二列标题</th>
                </tr>
                </thead> -->
                <tbody>
                <tr>
                <td><div contenteditable="true">第一行第一列</div></td>
                <td><div contenteditable="true">第一行第二列</div></td>
                <td><div contenteditable="true">第一行第三列</div></td>
                <td><div contenteditable="true">第一行第四列</div></td>
                <td><div contenteditable="true">第一行第五列</div></td>
                <td><div contenteditable="true">第一行第六列</div></td>
                <td><div contenteditable="true">第一行第七列</div></td>
                <td><div contenteditable="true">第一行第八列</div></td>
                </tr>
                </tbody>
                </table>
            </fieldset>
    <input id="num" type="text"  placeholder="请输入下一排的人数">
    <input type="button" onclick="addRow()" value="添加">

    <fieldset>
        <legend>填写示例</legend>
        <img src="images/uploadExample.jpg"/>
    </fieldset>

</div>
<script>
    /*
    * 因为集体有特殊的字段 所以单独搞成了一个添加资源的页(其实不一定非要添加)
    * */
    // 人数的验证没有做

    function addRow(){
            var num = jq1111('#num').val()
            console.log(num)
            var oTable = document.getElementById("oTable");
            var tBodies = oTable.tBodies;
            var tbody = tBodies[0];
            var tr = tbody.insertRow(tbody.rows.length);
            for (var i = 0; i <parseInt(num) ; i++) {
            var td_1 = tr.insertCell(i);
            td_1.innerHTML = "<div contenteditable='true'></div>";
            }
            /*
            var td_1 = tr.insertCell(0);
            td_1.innerHTML = "<div contenteditable='true'>第1列</div>";
            var td_2 = tr.insertCell(1);
            td_2.innerHTML = "<div contenteditable='true'>第2列</div>";
            var td_2 = tr.insertCell(2);
            td_2.innerHTML = "<div contenteditable='true'>第3列</div>";

             */
            }
    function getAllPeople(){
        var peopleList=[]
        jq1111('#oTable tr').each(function (i) {
            var curRow= []
            jq1111(this).children('td').each(function (j) {
                curRow.push(jq1111(this).text())
                console.log(jq1111(this).text())
            })
            peopleList.push(curRow)
        })
        return peopleList

    }



    layui.use(['form', 'table','laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            $ = layui.$,
            laydate = layui.laydate;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        /*
        无用
        table.on('edit(test)', function(obj){
            var value = obj.value //得到修改后的值
            ,data = obj.data //得到所在行所有键值
            ,field = obj.field; //得到字段
              console.log(value,data,field)
            layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
          });
         */

        form.render();

        //日期
        laydate.render({
            elem: '#date'
        });

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;
        jq1111('#rowlist').bind('keypress',function (event) {
            console.log('test')
        })



        //监听提交
        form.on('submit(saveBtn)', function (data) {
            getAllPeople();
            var albumId = localStorage.getItem("selectedAlbumId");
            var isPub = true;
            if (data.field.isPublic === 'off') {
                isPub = false;
            }
            var formData = new FormData();
            formData.append('isPublic',isPub)
            formData.append('albumId',albumId);
            formData.append('title',data.field.title);
            var description={
                "description": jq1111('#description').val(),
                "peopleList": getAllPeople(),
                "totalnumber": data.field.totalnumber,
                "realnumber": data.field.realnumber
            }

            formData.append('description',JSON.stringify(description));
            formData.append('shootingDate',data.field.date);
            formData.append('location',data.field.location);
            formData.append("file",jq1111("#upload-file")[0].files[0]);
            formData.append("tagId",data.field.tag);
            console.log(jq1111("#upload-file"))
            jq1111.ajax({
                type: "POST",
                crossDomain: true,
                url: BasePath+"api/resource/upload",
                processData: false,
                contentType : false,
                dataType: "json",
                xhrFields: {
                    withCredentials: true
                },
                data:formData,
                success: function (message) {
                    console.log(message);
                    console.log(JSON.stringify(message))
                    var index=layer.alert(message.msg, {
                        title: '操作结果'
                    },function (){
                        // 关闭弹出层
                        layer.close(index);
                        layer.close(parentIndex);
                    })
                },
                error: function (message) {
                    console.log(message.responseJSON);
                    layer.alert(message.responseJSON.msg, {
                        title: '操作结果'
                    })
                },
            });

            return false;
        });

        //获取所有TAG
        var tagData;
        jq1111.ajax({
            type: "GET",
            crossDomain: true,
            url: BasePath+"api/ResourceTag/",
            processData: false,
            contentType: false,
            dataType: "json",
            xhrFields: {
                withCredentials: true
            },
            success: function (message) {
                console.log(message);
                tagData=message.data;
                tagData.forEach(function (tag) {
                    jq1111("#tag-select").append(
                        '<option value="'+tag.id+'">'+tag.name+'</option>'
                    )
                });
                form.render();
            },
            error: function (message) {
                console.log(message.responseJSON);
            },
        });

    });

</script>